#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;

int n;
int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        cin >> n;
        if(n == 1){
            cout << 1 << '\n';
        }else if(n == 2){
            cout << 1 << '\n' << 1 << " " << 1 << '\n';
        }else{
            cout << 1 << '\n' << 1 << " " << 1 << '\n';
            for(int i = 3; i <= n; i ++){
                for(int j = 1; j <= i; j ++){
                    if(j == 1){
                        cout << 1 << " ";
                    }else if(j == i){
                        cout << 1;
                    }else{
                        cout << 0 << " ";
                    }
                }
                cout << '\n';
            }
        }
    }



    return 0;
}